package com.ywgt.system;

import com.ywgt.pojo.System;
import com.ywgt.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 从数据库读取数据，放入全局map中
 */
@Component
public class ConfigService {

    private static ConfigService configService;

    @Autowired
    private SystemService systemService;

    private ConfigService (){

    }
    @PostConstruct
    public void init(){
        configService=this;
        initConfigs();
    }
    static ConfigService getConfigService() {
        return configService;
    }

    public void reload(String prefix){
        List<System> configs = systemService.list();
        for (System config : configs) {
            if(config.getKeyName().startsWith(prefix)){
                BaseConfig.addConfig(config.getKeyName(),config.getKeyValue());
            }
        }
    }

    public void initConfigs(){
        List<System> configs = systemService.list();
        for (System config : configs) {
            BaseConfig.addConfig(config.getKeyName(),config.getKeyValue());
        }
    }


}
